/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: AcountImpl
 * Author:   pc
 * Date:     2019/9/18 16:53
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.rabbitmqdemo.service.impl;

import com.example.rabbitmqdemo.service.AcountService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 〈功能：〉<br> 
 * 〈〉
 * @author liuhaoran
 * @create 2019/9/18 16:53
 * @since 1.0.0
 */
@Service
public class AcountServiceImpl implements AcountService {
	//private ReentrantLock lock = new ReentrantLock();
	private int countsum=100;
	private boolean flag=false;    // 默认flag 为false，要求必须先存款再取款
	private int count;

	/**
	 * 存钱
	 * @param money
	 */
	public void addAcount(String name,int money) {
		//同步锁
//		lock.lock();
//		try{
//			// 存钱
//			count += money;
//			System.out.println(name+"...存入："+money);
//			SelectAcount(name);
//		}finally {
//			lock.unlock();
//		}

		//线程通信
		synchronized(this) {
			// flag 为true 表示可以存款，否则不可以存款
			if(flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				// 存钱
				count += money;
				System.out.println(name+"...存入："+money+"..."+Thread.currentThread().getName());
				SelectAcount(name);
				flag = true;
				this.notifyAll();
			}
		}
	}

	/**
	 * 取钱
	 * @param money
	 */
	public void subAcount(String name,int money) {
		//同步锁
//		lock.lock();
//		try{
//			// 先判断账户现在的余额是否够取钱金额
//			if(count-money < 0){
//				System.out.println("账户余额不足！");
//				return;
//			}
//			// 取钱
//			count -= money;
//			System.out.println(name+"...取出："+money);
//			SelectAcount(name);
//		}finally {
//			lock.unlock();
//		}
		//线程通信
		synchronized(this) {
			if(!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else {
				// 先判断账户现在的余额是否够取钱金额
				if(count-money < 0){
					System.out.println("账户余额不足！");
					return;
				}
				// 取钱
				count -= money;
				System.out.println(name+"...取出："+money+"..."+Thread.currentThread().getName());
				SelectAcount(name);
				flag = false;
				this.notifyAll();
			}
		}
	}

	/**
	 * 查询余额
	 */
	public void SelectAcount(String name) {
		System.out.println(name+"...余额："+count);
	}

	/**
	 *测试ABCD
	 * 可以通过同步 synchronized 解决线程安全问题
	 */
	@Async
	public synchronized void asynctest1(){
			try {
				Thread.sleep(1000);
			}catch (InterruptedException e){
				e.printStackTrace();
			}
			countsum=countsum-50;
			System.out.println("测试A:"+countsum);
	}
	@Async
	public synchronized void asynctest2(){
		try {
			Thread.sleep(1000);
		}catch (InterruptedException e){
			e.printStackTrace();
		}
		countsum=countsum+50;
		System.out.println("测试B:"+countsum);
	}
	@Async
	public synchronized void asynctest3(){
		try {
			Thread.sleep(1000);
		}catch (InterruptedException e){
			e.printStackTrace();
		}
		countsum = countsum-50;
		System.out.println("测试C:"+countsum);
	}
	@Async
	public void asynctest4(){
		System.out.println(countsum);
	}
}